package com.code2roc.jarmanage.servlet.redis;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.code2roc.jarmanage.model.Result;
import com.code2roc.jarmanage.util.CommonUtil;
import com.code2roc.jarmanage.util.ConvertUtil;
import com.code2roc.jarmanage.util.JedisUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.resps.Tuple;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

public class GetValueDetailServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result result = Result.okResult();
        HashMap params = CommonUtil.getParamMap(req);
        int dbIndex = ConvertUtil.convert2Int(params.get("dbIndex"));
        String key = ConvertUtil.convert2String(params.get("key"));
        Jedis client = JedisUtil.getClient();
        String keyType = JedisUtil.getKeyType(client,dbIndex, key,false);
        long keyExpire = JedisUtil.getKeyExpire(client,dbIndex, key,false);
        result.add("key", key);
        result.add("keyExpire", keyExpire);
        result.add("keyType", keyType);
        if (keyType.equals("string")) {
            String stringValue = JedisUtil.getString(key,client,dbIndex);
            result.add("value", stringValue);
        } else if (keyType.equals("list")) {
            JSONArray array = new JSONArray();
            List<String> listValue = JedisUtil.getList(key,client,dbIndex);
            for(int i=0;i<listValue.size();i++){
                JSONObject obj = new JSONObject();
                obj.put("index",i);
                obj.put("value",listValue.get(i));
                array.add(obj);
            }
            result.add("value", array);
        } else if (keyType.equals("set")) {
            JSONArray array = new JSONArray();
            Set<String> setValue = JedisUtil.getSet(key,client,dbIndex);
            for (String item:setValue) {
                JSONObject obj = new JSONObject();
                obj.put("value",item);
                array.add(obj);
            }
            result.add("value", array);
        } else if (keyType.equals("zset")) {
            List<Tuple> zsetValue = JedisUtil.getZset(key,client,dbIndex);
            JSONArray array = new JSONArray();
            for (Tuple item:zsetValue) {
                JSONObject obj = new JSONObject();
                obj.put("value",item.getElement());
                obj.put("score",item.getScore());
                array.add(obj);
            }
            result.add("value", array);
        } else if (keyType.equals("hash")) {
            Map hashValue = JedisUtil.getHash(key,client,dbIndex);
            JSONArray array = new JSONArray();
            for (Object hashKey:hashValue.keySet()) {
                JSONObject obj = new JSONObject();
                obj.put("key",ConvertUtil.convert2String(hashKey));
                obj.put("value",hashValue.get(hashKey));
                array.add(obj);
            }
            result.add("value", array);
        }
        resp.getWriter().println(result.toString());
    }
}
